const AV = require('../../libs/av-weapp-min.js');
//index.js

Page({
    time: 0,
    ctx: null,
    data: {
        user: null,
        site_arr: [{
            top: 180,
            left: 240,
            opacity: 0.2,
            direct: 1,
        }, {
            top: 400,
            left: 480,
            opacity: 0.4,
            direct: 1,

        }, {
            top: 640,
            left: 140,
            opacity: 0.6,
            direct: 1,
        }, {
            top: 960,
            left: 360,
            opacity: 0.8,
            direct: 1,
        }]
        ,
        start: false,
    },
    createRada(width, height, time) {
        if (!this.ctx) {
            this.ctx = wx.createCanvasContext('rada')
        }
        let ctx = this.ctx;
        //开始绘制波纹
        ctx.save();
        ctx.beginPath()
        ctx.translate(width / 2, height / 2);
        ctx.arc(0, 0, (time + 50) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 100) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 100) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 150) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 150) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 200) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 200) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 250) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 250) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 300) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 300) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 350) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 350) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 400) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 400) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.moveTo((time + 450) / 1.5 % 450, 0)
        ctx.arc(0, 0, (time + 450) / 1.5 % 450, 0, 2 * Math.PI, true);
        ctx.setStrokeStyle('red')
        ctx.setLineWidth(1);
        ctx.closePath()
        ctx.stroke();
        ctx.restore();
        //开始绘制线条
        ctx.save();
        ctx.translate(width / 2, height / 2);
        ctx.beginPath();
        ctx.moveTo(-2, 0);
        ctx.lineTo(500, 0);
        ctx.setStrokeStyle('blue')
        ctx.rotate((time / 3 % 360) * Math.PI / 180)
        ctx.setLineWidth(3);
        ctx.stroke();

        ctx.draw()
        ctx.restore();

        for (let i in this.data.site_arr) {
            let direct_key = `site_arr[${i}].direct`;
            let opacity_key = `site_arr[${i}].opacity`;
            if (this.data.site_arr[i].opacity > 0.999) {
                this.setData({ [direct_key]: -1 });
            } else if (this.data.site_arr[i].opacity < 0.1) {
                this.setData({ [direct_key]: 1 });
            }

            this.setData({ [opacity_key]: (this.data.site_arr[i].opacity + 0.03 * this.data.site_arr[i].direct) })
        }
    },
    scansite() {
        if (this.data.start) {
            return;
        }

        var c = setInterval(() => {
            this.time += 2;
            this.createRada(this.data.win_width, this.data.win_height, this.time);
        }, 25)

        //查找10公里内球场
        let location = wx.getStorageSync('location');
        var query = new AV.Query('site');
        var point = new AV.GeoPoint(location.latitude, location.longitude);
        query.withinKilometers('location', point, 10.0);
        query.find().then(results => {
            wx.setStorageSync('results', JSON.stringify(results.map(x => x.attributes)));
            setTimeout(() => {
                this.setData({start:false});
                console.log(results);
                clearInterval(c);
                this.ctx.clearRect(0, 0, this.data.win_width, this.data.win_height)
                this.ctx.draw()
                if (results.length < 1) {
                    wx.showModal({
                        title: '扫描结果',
                        content: '你的附近没有球场',
                        showCancel:false
                    })
                }else{
                wx.showModal({
                    title: '扫描结果',
                    content: `找到了！在你的附近共找到了${results.length}个球场`,
                    success: function (res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '/pages/map/map'
                            })
                        }
                    }
                })
                }
            }, 2000);
        }, function (error) {
        });

    },
    addsite() {
        wx.showActionSheet({
            itemList: ['添加当前位置', '添加自定义位置'],
            success: res => {
                let location = wx.getStorageSync('location');
                if (res.tapIndex === 0) {
                    let self = getCurrentPages()[getCurrentPages().length - 1].data;
                    wx.navigateTo({
                        url: `/pages/add/add?latitude=${location.latitude}&longitude=${location.longitude}`
                    })
                } else if (res.tapIndex === 1) {
                    wx.chooseLocation({
                        success: ({ address, latitude, longitude }) => {
                            wx.navigateTo({
                                url: `/pages/add/add?address=${address}&latitude=${latitude}&longitude=${longitude}`
                            })
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },
    onLoad() {
        let user = wx.getStorageSync('user');
        this.setData({ user: user });
        //设置控件位置
        wx.getSystemInfo({
            success: res => {
                this.setData({ 'map_height': res.windowHeight + 'px' });
                this.setData({ 'win_height': res.windowHeight });
                this.setData({ 'win_width': res.windowWidth });

            }
        });
    }
})

